<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    public function index() {
        $this->login();
    }

    public function login() {

        $this->load->view('user/login');
    }

    public function ajout() {
        $data['lesTeams'] = $this->user_model->lireTeams();
        $data['page'] = 'ajout';
        $this->load->view('template/template', $data);
    }    
    
    
public function traitementAjout()
	{
		$this->load->helper(array('form', 'url'));

		$this->load->library('form_validation');

		$this->form_validation->set_rules('pseudo', 'Pseudonyme', 'required');
		$this->form_validation->set_rules('login', 'Login', 'required');
		$this->form_validation->set_rules('prenom', 'Prenom', 'required');
		$this->form_validation->set_rules('nom', 'Nom', 'required');
		$this->form_validation->set_rules('age', 'Age', 'required');
		$this->form_validation->set_rules('ville', 'Ville', 'required');
		$this->form_validation->set_rules('emplacement', 'Emplacement', 'required');
		$this->form_validation->set_rules('password', 'Password', 'required');

		if ($this->form_validation->run() == FALSE)
		{
			$this->ajout();
		}
		else
		{
			$pseudo = $this->input->post('pseudo');
			$login = $this->input->post('login');
			$prenom = $this->input->post('prenom');
			$nom = $this->input->post('nom');
			$age = $this->input->post('age');
			$ddn = date('Y');
			$age = $ddn - $age;
			$ville = $this->input->post('Ville');
			$ville = $this->input->post('ville');
			$emplacement = $this->input->post('emplacement');
			$password = $this->input->post('password');
			$password = crypt($password, "laPhraseDeCryptage");
			$photo = $this->input->post('photo');
			$team = $this->input->post('team');
			$team = $this->input->post('selectTeam');
			$level = $this->input->post('selectAdmin');
			$user = new Userclass($login, $password, $pseudo, $nom, $prenom, $ddn, $ville, $emplacement, $photo, $team, $level);
			$error = $this->user_model->ajouter($user);
				
			if ($error == "")
			{
				$data['page']= 'ajoutok';
				$this->load->view('template/template',$data);
			}
			else {
				$data['page']= 'ajoutko';
				$this->load->view('template/template',$data);
			}
		}
	}

    public function validerLogin() {
        $login = $this->input->post("pseudonyme");
        $password = $this->input->post("password");
        $password = crypt($password, "laPhraseDeCryptage");
        $result = $this->user_model->checkLogin($login, $password);
        if (isset($result[0]) != NULL) {
            echo "ok";
            $this->session->set_userdata($result[0]);
            $arr = $this->session->all_userdata();
        } 
        else {
            echo "login  ou mdp incorect";
        }
        redirect("index", "refresh");
        }

    
    public function annuaire()
    {
    	$data['lesGames'] = $this->user_model->lireGames();
    	$data['lesUsers'] = $this->user_model->readAll();
    	$data['page']= 'annuaire';
    	$this->load->view('template/template', $data);
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */